// pages/operat/fleedTools/unBoxing/unBoxing.js
const LApp = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
   unBoxingModel: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = LApp.pages.get("fleedTools/unBoxing/scanCode/scanCode").data;
    this.setData({
      unBoxingModel: data.AllocInfo
    })
      this.GetFormCode();
      this.productShow(this.data.unBoxingModel.ProductID);
      this.FromWarehouseShow(this.data.unBoxingModel.FromWarehouseId);
      this.getGoWarehouseList(this.data.unBoxingModel.FromWarehouseId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
 
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  GetFormCode: function (data) {
    var formCode = LApp._formatTime("yyyyMMddhhmmss");
    if (data) {
      formCode = data + "-" + formCode;
    }
    this.data.unBoxingModel.FormCode = formCode;
    this.setData(this.data);
  },
  //调出库房下拉框
  getGoWarehouseList: function (fId) {
   let _this = this;
   LApp._get({
     url: 'nbapi/warehouse/gowarehouseselect',
     data: { userId: LApp.globalData.userInfo.UserID, warehouseId: fId},
    success: function (r) {
     _this.setData({
      goWarehouseList: r
     })
    }
   })
  },
  goWarehouseSelect: function (e) {
   var _goWarehouse = this.data.goWarehouseList[e.detail.value];
   if (_goWarehouse) {
    this.data.unBoxingModel.gName = _goWarehouse.Name;
    this.data.unBoxingModel.GoWarehouseId = _goWarehouse.Code;
    this.setData(this.data);
    var fromId = this.data.unBoxingModel.FromWarehouseId;
    fromId = fromId == undefined ? 0 : fromId;
    this.GetFormCode(fromId + "-" + _goWarehouse.Code);
   }
  }, 
  
  productShow: function (proId) {
    let _this = this;
    LApp._get({
      url: 'nbapi/product/showproductinfo',
      data: { proId: proId },
      success: function (res) {
        _this.data.unBoxingModel.pName = res.ProductName;
        _this.data.unBoxingModel.ProductID = res.Id;
        _this.setData(_this.data);
      }
    })
  },
  FromWarehouseShow: function (warehouseId) {
    let _this = this;
    LApp._get({
      url: 'nbapi/fleedtool/warehouse/getmodel',
      data: { Id: warehouseId},
      success: function (res) {
        _this.data.unBoxingModel.fName = res[0].Name;
        _this.setData(_this.data);
      }
    })
  },
  bindModelChange: function (e) {
   wx.$$apply(e, this.data.unBoxingModel);
  },
  saveBoxing: function () {
    if (this.data.unBoxingModel.GoWarehouseId == undefined){
wx.showModal({
  title: '',
  content: '请选择调入仓库',
})
return;
    }
    this.data.unBoxingModel.CreateLoginID = LApp.globalData.userInfo.UserID;
    this.data.unBoxingModel.CreateLoginName = LApp.globalData.userInfo.LoginName;
    this.data.unBoxingModel.EnterpriseID = LApp.globalData.userInfo.EnterpriseId;
   LApp._post({
    url: 'nbapi/product/unboxing/add',
    data: this.data.unBoxingModel,
    success: function (r) {
      wx.showModal({
        title: '保存成功！',
        icon: "success",
        success: function (res) {
          if (!res.cancel) {
            var pages = getCurrentPages();
            var currPage = pages[pages.length - 1];   //当前页面
            var prevPage = pages[pages.length - 2];  //上一个页面
            prevPage.setData({
              myDate: 1
            });
            wx.navigateBack(1);
          }
        }
      })
    }
   })
  }, 
  goSearchCode: function () {
   if (this.data.unBoxingModel) {
   LApp.pages.add(this);
   wx.navigateTo({
    url: 'searchCode/searchCode',
   })
  }
 }
})